﻿using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

namespace SIS.DAL
{
    /// <summary>
    /// The ClassRoomTableDB class is responsible for interacting with the database to retrieve and store information
    /// about ClassRoomTable objects.
    /// </summary>
    public class ListingDB
    {
        #region "Public Methods"
        /// <summary>
        /// Returns a list with ClassRoomTable objects.
        /// </summary>
        /// <returns>A generics List with the ClassRoomTable objects.</returns>
        public static ListingList GetList(int ListType)
        {
            ListingList tempList = new ListingList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSelectListing", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@ListType", ListType.ToString());
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Initializes a new instance of the ClassRoomTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static Listing FillDataRecord(IDataRecord myDataRecord)
        {
            Listing myListing = new Listing();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ListValue")))
            {
                myListing.ListValue = myDataRecord.GetString(myDataRecord.GetOrdinal("ListValue"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ListText")))
            {
                myListing.ListText = myDataRecord.GetString(myDataRecord.GetOrdinal("ListText"));
            }

            return myListing;
        }

        #endregion
    }

    
}
